<?php

/*
 * voting screen
 */

//grab items in order (only once)
$items = new items();
if(!isset($_SESSION['items']) || empty($_SESSION['items']) || $_GET['reload'] == TRUE) {
	if($itemList = $items->find(array('order' => 'category,name'))) {
		if(!isset($itemList[0])) $itemList = array($itemList);
		$_SESSION['items'] = $itemList;
		$_SESSION['curItem'] = 0;
	} else {
		die('empty');
	}
}

$doc = Control::getView();
$doc->loadTemplate(htmlDir ."template.html");
$header = $doc->getPathFirst('div','id','Header');
$content = $doc->getPathFirst('div','id','Content');

//init votes obj
$votes = new votes();

if($_SERVER['REQUEST_METHOD'] == "POST") {
	//insert vote
	$curItem = $_SESSION['items'][$_SESSION['curItem']];
	$addParams = array_merge($_POST,array("itemId" => $curItem['itemId']));
	if(!$votes->add($addParams)) {
		//success msg
		die('Error voting');
	}
} elseif(isset($_GET['setItem']) && is_numeric($_GET['setItem']) && $_GET['setItem'] > 0 && $_GET['setItem'] <= count($_SESSION['items'])) {
	$_SESSION['curItem'] = ($_GET['setItem'] - 1);
} elseif(isset($_GET['next']) && $_GET['next'] == TRUE && ($_SESSION['curItem'] + 1) < count($_SESSION['items'])) {
	$_SESSION['curItem']++;
} elseif(isset($_GET['previous']) && $_GET['previous'] == TRUE && ($_SESSION['curItem'] - 1) >= 0) {
	$_SESSION['curItem']--;
} elseif(isset($_GET['done']) && $_GET['done'] == TRUE) {
	//display results
	$votes->join($items, array('on' => array('items.itemId' => 'votes.itemId')));
	if($results = $votes->find(array('select' => '(SUM( taste ) / count( * )  + SUM( originality ) / count( * )  + SUM( presentation ) / count( * ) ) AS total, items.*',
									 'group' => 'items.itemId',
									 'order' => 'total DESC'))) {
		//print_r($results);
		$totalMarkup = '<h3>Total Results</h3>';
		foreach($results as $res) {
			$guessed = 0;
			if($guessRes = $votes->find(array('select' => 'count(*) as guessed', 'where' => "votes.itemId = ".$res['itemId']." AND votes.guess LIKE '".addslashes($res['name'])."'"))) {
				if($guessRes['guessed'] > 0) $guessed = $guessRes['guessed'];
			}
			$totalMarkup .= 'Beer Id:'.$res['itemId'].' Total Score: '.$res['total'].', Beer: '.$res['name'].', Owner: '.$res['owner'].', Category: '.$res['category'].', Guessed: '.$guessed.'<br>';
			
		}
		$doc->appendString($content,$totalMarkup);
	}
	
	if($tasteRes = $votes->find(array('select' => 'SUM( taste )/ count( * ) AS total, items.*',
									 'group' => 'items.itemId',
									 'order' => 'total DESC'))) {
		//print_r($tasteRes);
		$tasteMarkup = '<h3>By Taste</h3>';
		foreach($tasteRes as $res) {
			$tasteMarkup .= 'Beer Id:'.$res['itemId'].' Total Score: '.$res['total'].', Beer: '.$res['name'].', Owner: '.$res['owner'].'<br>';
		}
		$doc->appendString($content,$tasteMarkup);
	}
	
	if($origRes = $votes->find(array('select' => 'SUM( originality )/ count( * ) AS total, items.*',
									 'group' => 'items.itemId',
									 'order' => 'total DESC'))) {
		//print_r($origRes);
		$origMarkup = '<h3>By Originality</h3>';
		foreach($origRes as $res) {
			$origMarkup .= 'Beer Id:'.$res['itemId'].' Total Score: '.$res['total'].', Beer: '.$res['name'].', Owner: '.$res['owner'].'<br>';
		}
		$doc->appendString($content,$origMarkup);
	}
	
	if($presRes = $votes->find(array('select' => 'SUM( presentation )/ count( * ) AS total, items.*',
									 'group' => 'items.itemId',
									 'order' => 'total DESC'))) {
		//print_r($presRes);
		$presMarkup = '<h3>By Presentiation</h3>';
		foreach($presRes as $res) {
			$presMarkup .= 'Beer Id:'.$res['itemId'].' Total Score: '.$res['total'].', Beer: '.$res['name'].', Owner: '.$res['owner'].'<br>';
		}
		$doc->appendString($content,$presMarkup);
	}
	print $doc->saveHTML();
	die;
}

//show current item
//$header->appendChild($doc->createTextNode('Current Beer No. : '.($_SESSION['curItem'] + 1)));
//$header->appendChild($doc->br());
$header->appendChild($doc->createTextNode('Current Beer Id. : '.$_SESSION['items'][$_SESSION['curItem']]['itemId']));

//show amount of votes for item
if($voteCountRes = $votes->find(array('select' => 'count(*) as voteCount', 'where' => 'itemId = '.$_SESSION['items'][$_SESSION['curItem']]['itemId']))) {
	$header->appendChild($doc->br());
	$header->appendChild($doc->createTextNode('Vote Count : '.$voteCountRes['voteCount']));
}

//display first item to vote on
$voteHTML = file_get_contents(htmlDir ."votes.html");
$doc->appendString($content,$voteHTML);

?>